package com.example.eldersystem.controller;

import com.example.eldersystem.response.DataResponse;
import com.example.eldersystem.operatedata.model.Volunteer;
import com.example.eldersystem.operatedata.service.VolunteerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;


@RestController
@RequestMapping("/volunteer")
public class VolunteerController {

    @Autowired
    VolunteerService volunteerService;

    @RequestMapping("/add")
    private DataResponse<String> addVolunteer(@RequestBody Map<String,Object> map){
        DataResponse<String> dataResponse = new DataResponse<>();
        Volunteer volunteer = new Volunteer();
        Date udate = null;
        Date checkIn =null;
        Date checkOut = null;

        String birthday = (String) map.get("birthday");
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            udate = format.parse(birthday);
        }catch (ParseException e){
            dataResponse.setMsg("日期格式有误");
            return dataResponse;
        }
        volunteer.setBirthday(udate);

        String checkInDate = map.get("checkinDate").toString();
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            checkIn = format.parse(checkInDate);
        }catch (ParseException e){
            dataResponse.setMsg("开始日期格式有误");
            return dataResponse;
        }
        volunteer.setCheckinDate(checkIn);

        if(map.get("checkoutDate")!=null) {
            String checkOutDate = map.get("checkoutDate").toString();
            if (!checkOutDate.equals("")) {
                try {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    checkOut = format.parse(checkOutDate);
                } catch (ParseException e) {
                    dataResponse.setMsg("结束日期格式有误");
                    return dataResponse;
                }
                if (checkOut.after(checkIn)) {
                    volunteer.setCheckoutDate(checkOut);
                } else {
                    dataResponse.setMsg("出院日期不得早于等于入院时期");
                    return dataResponse;
                }
            }
        }

        volunteer.setDescription(map.get("description").toString());
        volunteer.setName(map.get("name").toString());
        volunteer.setGender(map.get("gender").toString());
        volunteer.setIdCard(map.get("idCard").toString());
        volunteer.setPhone(map.get("phone").toString());
        volunteer.setImgsetDir(map.get("imgsetDir").toString());
        volunteer.setProfilePhoto(map.get("profilePhoto").toString());

        dataResponse = volunteerService.addVolunteer(volunteer);
        return dataResponse;
    }


    @RequestMapping("/delete")
    private DataResponse<String> deleteVolunteer(@RequestBody Map<String,Object> map){
        DataResponse<String> dataResponse = new DataResponse<>();
        int id = Integer.parseInt(map.get("id").toString());
        dataResponse = volunteerService.deleteVolunteer(id);
        return dataResponse;
    }

    @RequestMapping("/update")
    private DataResponse<String> updateVolunteer(@RequestBody Map<String,Object> map){
        DataResponse<String> dataResponse = new DataResponse<>();
        Volunteer volunteer = new Volunteer();
        Date udate = null;
        Date checkIn =null;
        Date checkOut = null;

        String birthday = (String) map.get("birthday");
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            udate = format.parse(birthday);
        }catch (ParseException e){
            dataResponse.setMsg("日期格式有误");
            return dataResponse;
        }
        volunteer.setBirthday(udate);

        String checkInDate = map.get("checkinDate").toString();
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            checkIn = format.parse(checkInDate);
        }catch (ParseException e){
            dataResponse.setMsg("开始日期格式有误");
            return dataResponse;
        }
        volunteer.setCheckinDate(checkIn);

        if(map.get("checkoutDate")!=null) {
            String checkOutDate = map.get("checkoutDate").toString();
            if (!checkOutDate.equals("")) {
                try {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    checkOut = format.parse(checkOutDate);
                } catch (ParseException e) {
                    dataResponse.setMsg("结束日期格式有误");
                    return dataResponse;
                }
                if (checkOut.after(checkIn)) {
                    volunteer.setCheckoutDate(checkOut);
                } else {
                    dataResponse.setMsg("出院日期不得早于等于入院时期");
                    return dataResponse;
                }
            }
        }
        volunteer.setId(Integer.parseInt(map.get("id").toString()));
        volunteer.setDescription(map.get("description").toString());
        volunteer.setName(map.get("name").toString());
        volunteer.setGender(map.get("gender").toString());
        volunteer.setIdCard(map.get("idCard").toString());
        volunteer.setPhone(map.get("phone").toString());
        volunteer.setImgsetDir(map.get("imgsetDir").toString());
        volunteer.setProfilePhoto(map.get("profilePhoto").toString());

        dataResponse = volunteerService.updateVolunteer(volunteer);
        return dataResponse;
    }

    @RequestMapping("/count")
    private DataResponse<Integer> getVolunteerNum(){
        DataResponse<Integer> dataResponse = new DataResponse<>();
        dataResponse.setData(volunteerService.getVolunteerNum());
        return dataResponse;
    };

    @RequestMapping("/select")
    private DataResponse<List<Volunteer>> getVolunteer(@RequestBody Map<String,Object> map){
        DataResponse<List<Volunteer>> dataResponse = new DataResponse<>();
        int from = 0;
        int num = 0;
        try{
            from = Integer.parseInt(map.get("from").toString());
            num = Integer.parseInt(map.get("num").toString());
        }catch(Exception e){
            dataResponse.setMsg("请求错误");
            return dataResponse;
        }
        dataResponse=volunteerService.selectVolunteer(from,num);
        return dataResponse;
    };

    @RequestMapping("/search")
    private DataResponse<List<Volunteer>> searchVolunteer(@RequestBody Map<String,Object> map){
        DataResponse<List<Volunteer>> dataResponse = new DataResponse<>();
        String name;
        try{
            name = map.get("name").toString();
        }catch(Exception e){
            dataResponse.setMsg("请求错误");
            return dataResponse;
        }
        dataResponse=volunteerService.selectVolunteerByName(name);
        return dataResponse;
    };

    @RequestMapping("/changePhoto")
    private DataResponse<String> updateLogoImage(@RequestBody Map<String,Object> map){
        DataResponse<String> dataResponse = new DataResponse<>();
        dataResponse.setCode(0);
        int id=Integer.parseInt(map.get("id").toString());
        String profilePhoto=map.get("profilePhoto").toString();
        dataResponse = volunteerService.updateProfile(id,profilePhoto);
        return dataResponse;
    }
}
